
package com.lzb.compressmaster.dialog;

import android.content.Context;
import android.os.Environment;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.DocumentsAdapter;
import com.lzb.compressmaster.basic.BaseSmartDialog;
import com.lzb.compressmaster.basic.media.MediaInfo;
import com.lzb.compressmaster.databinding.DialogCopyBinding;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CopyDialog extends BaseSmartDialog<DialogCopyBinding> implements View.OnClickListener, OnItemClickListener {

    private boolean isCopy;
    private List<MediaInfo> listShow = new ArrayList<>();
    private DocumentsAdapter documentsAdapter;
    private String currentRootPath;

    public CopyDialog(@NonNull Context context) {
        super(context);
    }

    public void setCopy(boolean copy) {
        isCopy = copy;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_copy;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvAddFolder.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new LinearLayoutManager(getContext()));
        documentsAdapter = new DocumentsAdapter();
        mDataBinding.rvFileList.setAdapter(documentsAdapter);
        documentsAdapter.setOnItemClickListener(this);

        currentRootPath = Environment.getExternalStorageDirectory().getPath();
        getData(currentRootPath);

        if (isCopy) {
            mDataBinding.tvConfirm.setText(getContext().getText(R.string.copy_it_here));
        } else {
            mDataBinding.tvConfirm.setText(getContext().getText(R.string.move_it_here));
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvAddFolder) {
            showAddFolderDialog();
        } else if (id == R.id.tvConfirm) {
            dismiss();
            if (listener != null) {
                if (isCopy) {
                    listener.clickCopy(currentRootPath);
                } else {
                    listener.clickMove(currentRootPath);
                }
            }
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        String path = documentsAdapter.getItem(position).getPath();
        if (FileUtils.isDir(path)) {
            currentRootPath = path;
            getData(currentRootPath);
        }
    }

    private void getData(String rootPath) {
        mDataBinding.tvAddress.setText(rootPath);
        listShow.clear();
        List<File> files = FileUtils.listFilesInDir(rootPath);
        if (files.size() == 0) {
            mDataBinding.flFileNoData.setVisibility(View.VISIBLE);
            mDataBinding.rvFileList.setVisibility(View.GONE);
        } else {
            mDataBinding.flFileNoData.setVisibility(View.GONE);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            for (File file : files) {
                listShow.add(new MediaInfo(file.getPath(), file.getName(), file.length(),
                        FileUtils.getFileLastModified(file), null, null));
            }
            mDataBinding.rvFileList.scrollToPosition(0);
            documentsAdapter.setList(listShow);
        }
    }

    private void addFolder(String name) {
        File file = new File(currentRootPath, name);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (mkdirs) {
                ToastUtils.showShort(R.string.add_dir_s);
                getData(currentRootPath);
            } else {
                ToastUtils.showShort(R.string.add_dir_s);
            }
        } else {
            ToastUtils.showShort(R.string.is_dir);
        }
    }

    private void showAddFolderDialog() {
        AddFolderDialog addFolderDialog = new AddFolderDialog(getContext());
        addFolderDialog.setListener(new AddFolderDialog.Ilistener() {
            @Override
            public void clickAddFolder(String name) {
                addFolder(name);
            }
        });
        addFolderDialog.show();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickCopy(String path);

        void clickMove(String path);
    }
}
